package com.link.gateway.base.functiontime.service;

import javax.annotation.Resource;

import com.link.gateway.base.functiontime.model.FunctionTime;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 功能耗时帮助类-功能耗时帮助类
 *
 * @author 秦学刚
 * @date 2019/04/10 15:24
 */
@Service
public class FunctionTimeServiceImpl implements FunctionTimeService {

    @Resource
    private JdbcTemplate jdbcTemplate;


    /**
     * 新建一条数据
     *
     * @param entity
     */
    @Override
    public void insert(List<FunctionTime> entity) throws Exception {
        //构造插入语句的StringBuilder对象
        StringBuilder sql = new StringBuilder();
        sql.append("insert into lnk_function_time(FUNCTION_CODE, FUNCTION_NAME, START_TIME, END_TIME, FUNCTION_TIME) values(?,?,?,?,?)");

        //获取得到数据库连接信息
        jdbcTemplate.batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                FunctionTime functionTime = entity.get(i);
                ps.setString(1, functionTime.getFunctionCode());
                ps.setString(2, functionTime.getFunctionName());
                ps.setString(3, functionTime.getStartTime());
                ps.setString(4, functionTime.getEndTime());
                ps.setString(5, String.valueOf(functionTime.getFunctionTime()));
            }

            @Override
            public int getBatchSize() {
                return entity.size();
            }
        });
    }
}
